<template>
    <view>
        <view class="page-section">
            <view class="form-row">
                <view class="form-row-label"><label for="controlled">收件人</label></view>
                <view class="form-row-content">
                    <input class="input" id="controlled" onInput="account" placeholder="请输入收件人" />
                </view>
            </view>

            <view class="form-row">
                <view class="form-row-label"><label for="controlled">收货地址</label></view>
                <view class="form-row-content">
                    <input class="input" id="controlled" onInput="address" placeholder="请输入收货地址" />
                </view>
            </view>

            <view class="form-row">
                <view class="form-row-label"><label for="controlled">电话号</label></view>
                <view class="form-row-content">
                    <input class="input" id="controlled" onInput="phone" placeholder="请输入电话号" />
                </view>
            </view>
        </view>
        <view class="disi">
            <ant-button type="primary" onTap="handleTap">保存</ant-button>
        </view>
    </view>
</template>

<script>

export default {
    components: {

    },
    data() {
        return {
            account: '',
            address: '',
            addressid: 0,
            isdefault: 0,
            openID: '',
            phone: '',
            focus: false
        };
    },
    onLoad() {
        // Fetch the openID from storage
        uni.getStorage({
            key: 'openId',
            success: (res) => {
                this.setData({
                    openID: res.data
                });
                this.fetchExistingData(res.data);
            },
            fail: (err) => {
                console.log(err);
            }
        });
    },
    methods: {
        fetchExistingData(openID) {
            uni.request({
                url: url + '/selectAllAddress',
                method: 'GET',
                data: {
                    openID
                },
                headers: {
                    'content-type': 'application/json'
                },
                dataType: 'json',
                success: (res) => {
                    if (res.data && res.data.success) {
                        this.setData({
                            account: res.data.account,
                            address: res.data.address,
                            phone: res.data.phone,
                            addressid: res.data.addressid,
                            isdefault: res.data.isdefault
                        });
                    } else {
                        console.log('No existing data found.');
                    }
                },
                fail: (error) => {
                    console.error('Failed to fetch existing data:', JSON.stringify(error));
                }
            });
        },

        onBlur() {
            this.setData({
                focus: false
            });
        },

        // 处理姓名输入
        accountFun(e) {
            this.setData({
                account: e.detail.value
            });
        },

        // 处理收货地址输入
        addressFun(e) {
            this.setData({
                address: e.detail.value
            });
        },

        // 处理手机号输入
        phoneFun(e) {
            this.setData({
                phone: e.detail.value
            });
        },

        // 保存按钮点击事件
        handleTap() {
            const { accountFun: account, addressFun: address, phoneFun: phone, openID, addressid, isdefault } = this;

            // 校验数据（可选）
            if (!account || !address || !phone) {
                uni.alert({
                    content: '请填写完整的表单信息'
                });
                return;
            }

            // 发送请求保存修改后的数据
            uni.request({
                url: url + '/updateByIDAddress',
                // API 接口地址
                method: 'POST',
                data: {
                    account,
                    address,
                    phone,
                    openID,
                    addressid,
                    isdefault
                },
                headers: {
                    'content-type': 'application/json' // 默认值
                },
                dataType: 'json',
                success: (res) => {
                    console.log('Success:', res.data);
                    if (res.data.success) {
                        uni.alert({
                            content: '保存成功'
                        });
                        uni.switchTab({
                            url: '/pages/myPage/myPage' // Navigate to the MyPage
                        });
                    } else {
                        uni.alert({
                            content: '保存失败'
                        });
                    }
                },
                fail: (error) => {
                    console.error('Fail:', JSON.stringify(error));
                },
                complete: (res) => {
                    uni.hideLoading();
                }
            });
        }
    }
};
</script>
<style>
.buttons {
    background: #ffffff;
    padding: 12px 12px 48px 12px;
}
.disi {
    height: 100px;
    width: 90%;
    bottom: 0;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    position: absolute;
    left: 20px;
    right: 10px;
}

.search-outer {
    box-sizing: border-box;
    display: flex;
    height: 40px;
    overflow: hidden;
    padding: 8px;
    border-bottom: 1px solid #ddd;
    background-color: #efeff4;
}

.search-outer * {
    box-sizing: border-box;
}

.search-input {
    flex: 1;
    text-align: left;
    display: block;
    color: #000;
    height: 24px;
    font-size: 15px;
    background-color: #fff;
    border-color: transparent;
}

.search-input:focus + .search-cancel {
    margin-right: 0;
    opacity: 1;
}

.search-cancel {
    margin-right: -40px;
    display: inline-block;
    opacity: 0;
    padding-left: 8px;
    height: 24px;
    line-height: 24px;
    font-size: 16px;
    color: #108ee9;
    text-align: right;
    transition: margin-right 0.3s, opacity 0.3s;
    transition-delay: 0.1s;
}

</style>
